﻿
// <copyright file="Extensions.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The extensions.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Extensions
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The extensions.
    /// </summary>
    public static class Extensions
    {
        #region Public Methods and Operators

        /// <summary>
        /// The build hash set.
        /// </summary>
        /// <typeparam name="T1">
        /// The type of the 1.
        /// </typeparam>
        /// <typeparam name="T2">
        /// The type of the 2.
        /// </typeparam>
        /// <param name="enumerable">
        /// The enumerable.
        /// </param>
        /// <param name="getValue">
        /// The get value.
        /// </param>
        /// <returns>
        /// The <see cref="HashSet"/>.
        /// </returns>
        public static HashSet<T2> BuildHashSet<T1, T2>(this IEnumerable<T1> enumerable, Func<T1, T2> getValue)
        {
            var hashSet = new HashSet<T2>();
            foreach (T1 item in enumerable)
            {
                T2 value = getValue(item);
                if (value != null)
                {
                    hashSet.Add(value);
                }
            }

            return hashSet;
        }

        /// <summary>
        /// The build hash set of value type.
        /// </summary>
        /// <typeparam name="T1">
        /// The type of the 1.
        /// </typeparam>
        /// <typeparam name="T2">
        /// The type of the 2.
        /// </typeparam>
        /// <param name="enumerable">
        /// The enumerable.
        /// </param>
        /// <param name="getValue">
        /// The get value.
        /// </param>
        /// <returns>
        /// The <see cref="HashSet"/>.
        /// </returns>
        public static HashSet<T2> BuildHashSetOfValueType<T1, T2>(this IEnumerable<T1> enumerable, Func<T1, T2?> getValue) where T2 : struct
        {
            var hashSet = new HashSet<T2>();
            foreach (T1 item in enumerable)
            {
                T2? value = getValue(item);
                if (value != null)
                {
                    hashSet.Add(value.Value);
                }
            }

            return hashSet;
        }

        /// <summary>
        /// To the hash set.
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <param name="enumerable">
        /// The enumerable.
        /// </param>
        /// <returns>
        /// The <see cref="HashSet"/>.
        /// </returns>
        public static HashSet<T> ToHashSet<T>(this IEnumerable<T> enumerable)
        {
            var hashSet = new HashSet<T>();
            foreach (T item in enumerable)
            {
                hashSet.Add(item);
            }

            return hashSet;
        }

        #endregion Public Methods and Operators
    }
}